package app.auto.runner.base.widget;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;

import com.aliang.auto.R;

import java.util.Calendar;
import java.util.Date;

import app.auto.AndroidInstance;
import app.auto.runner.base.DialogUtil;
import app.auto.runner.base.intf.FunCallback;

public class DatePicker extends AndroidInstance {
    private Date selectedDate;

    public  void pick(final FunCallback funCallback) {
        View view = getCalendarView(funCallback);
        DialogUtil.showDialog(view, view, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                funCallback.onCallback(selectedDate,null);
                dialogInterface.cancel();
                dialogInterface.dismiss();
            }
        }).show();
    }


    public  View getCalendarView(final FunCallback funCallback) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_date, null);
        android.widget.DatePicker datePicker = (android.widget.DatePicker) view.findViewById(R.id.dpPicker);

        Calendar calendar = Calendar.getInstance();
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new android.widget.DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(android.widget.DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                // 获取一个日历对象，并初始化为当前选中的时间
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);

                selectedDate = calendar.getTime();

            }
        });
        return view;
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
